<?php

/**
 * AutoCompleteBehavior class
 *
 * @author Julot
 * @since Sunday, June 17, 2012. 09:54 PM
 */
class AutoCompleteBehavior extends CActiveRecordBehavior {

	public function autoCompleteData($attribute, $data = array()) {
		$con = $this->owner->dbConnection;
		$column = $con->quoteColumnName($attribute);
		$rows = $con->createCommand()
			->selectDistinct($attribute)
			->from($this->owner->tableName())
			->order($attribute)
			->where("$column IS NOT  NULL")
			->queryColumn();
		if ($rows) {
			$data = array_merge($data, $rows);
			$data = array_unique($data);
		}
		sort($data);
		return $data;
	}

}
